<?php
/**
 +------------------------------------------------------------------------------
 * DepotAction控制器类
 +------------------------------------------------------------------------------
 * @category   SubAction
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <admin@iic.cn>
 * @date 2013-8-19
 * @time  下午3:35:06
 +------------------------------------------------------------------------------
 */
class DepotAction extends BaseAction{
	function _initialize() {
		parent::_initialize();
		$this->dao=M("Depot");
		$depotstatus=array();
		//0取消  1编辑中  2竞标中  3竞标结束
		$depotstatus['0']='取消';
		$depotstatus['1']='分配中';
		$depotstatus['2']='竞标中';
		$depotstatus['3']='竞标结束';
		$this->assign('depotstatus',$depotstatus);
	}
	/**
	*出库单列表
	*@date 2013-8-19
	*@time 下午3:35:31
	*/
	function index() {
	//出库单列表
		$where=array();
		$group='';
		$sel_status=$_REQUEST['sel_status'];
		if($sel_status === null || $sel_status === ''){
			$sel_status = 'ALL';
		}
		$this->assign('sel_status',$sel_status);
		if($sel_status!='ALL'){
			$where['status']=$sel_status;
		}
		$g_field=$_REQUEST['g_field'];
		$g_field=empty($g_field)?'0':$g_field;
		$this->assign('g_field',$g_field);
		
		if($g_field!='0'){
			$group=$g_field;
		}
		$oid=$_REQUEST['oid'];
		if($oid){
			$where['order_id']=$oid;
			$this->assign('oid',$oid);
		}
		$id=$_REQUEST['id'];
		if($id){
			$where['id']=$id;
		}
		$code=$_REQUEST['code'];
		if($code){
			$where['code']=$code;
		}
		
		$where = self::create_where($where);
		
		$field="id,order_id,goods_id,product_id,code,ctime,mtime,action_userid,status";
		$list=array();
		$dao=M('Depot');
		if(!empty($group)){
			//合并订单内商品
			$field.=",sum(`number`) number";
			$count=$dao->where($where)->group($group)->count();			
			if($count>0){
				$page=new Page($count,$this->listrows);
				$list=$dao->field($field)->where ( $where )->group($group)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select ();
			}
		}else{
			//不合并订单内商品
			$field.=",number";
			$count=$dao->where($where)->count();
			if($count>0){
				$page=new Page($count,$this->listrows);
				$list=$dao->field($field)->where ( $where )->order($this->order)->limit($page->firstRow.','.$page->listRows)->select ();
			}
		}
		$this->assign("totalCount",$count);
		$this->assign('list',$list);
		$productid=arr_to_explode($list,'product_id');
		$oids=arr_to_explode($list,'order_id');
		$product=self::getgoods($oids,$productid);
		$this->assign('product',$product);
		$ids=arr_to_explode($list,'id');
		$supnumber=self::supnumber($ids);
		$this->assign('supnumber',$supnumber);
		$this->display();
	}//end index
	
	/**
	*查看供应商出价
	*@date 2013-8-20
	*@time 下午10:21:44
	*/
	function more() {
	//查看供应商出价
		$where=array();
		$dao=M("DepotMore");
		$oid=$_REQUEST['oid'];
		$url=array();
		if($oid){
			$url['oid']=$oid;
			$ids=self::getorder_depot($oid);
			$ids=arr_to_explode($ids,'id',',');
			$where['depot_id']=array('IN',$ids);
			$this->assign('oid',$oid);
		}
		$id=$_REQUEST['id'];
		if($id){
			$url['id']=$id;
			$where['depot_id']=$id;
		}
		$code=$_REQUEST['code'];
		if($code){
			$url['code']=$code;
			$where['code']=$code;
		}
		$this->assign('urlencode',serialize($url));
		$where['is_show']=array('GT',0);
	 	$count=$dao->where($where)->count();
        $this->assign("totalCount",$count);
        if($count>0){
            $page=new Page($count,$this->listrows);
            $list=$dao->where($where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
            $this->assign('list',$list);
        }
        $depotids=arr_to_explode($list,'depot_id');
        $supids=arr_to_explode($list,'suppliers');
        $sups=self::getsups($supids);
        $this->assign('sups',$sups);
        $depotinfo=self::getdepotinfo($depotids);
        $this->assign('depotinfo',$depotinfo);
        $goodsids=arr_to_explode($depotinfo,'product_id');
        $oids=arr_to_explode($depotinfo,'order_id');
        $product=self::getgoods($oids,$goodsids,'product_id');
        $this->assign('product',$product);
        $status=array();
        //-1未出价 0缺货  1未中标  2已经中标  3已经出库
        $status['-1']='未出价';
        $status['0']='缺货';
        $status['1']='未中标';
        $status['2']='已经中标';
        $status['3']='已经出库';
        $status['5']='询价中';
        $this->assign('status',$status);
		$this->display();
	}//end more
	
	/**
	*确认中标界面
	*@date 2013-8-20
	*@time 下午11:23:27
	*/
	function more_edit() {
	//确认中标界面
		$id=intval($_REQUEST['id']);
		if(empty($id)){
			$this->_ajm(300,'请选择一条需要修改的数据',0);
		}
		$this->assign('urlencode',$urlencode);
		$dao=M("DepotMore");
		$info=$dao->where("id=".$id)->find();
		if(empty($info)){
			$this->_ajm(300,'操作错误，请重试',0);
		}
		$this->assign("info",$info);
		$status=array();
		//-1未出价 0缺货  1未中标  2已经中标  3已经出库
		$status['-1']='未出价';
		$status['0']='缺货';
		$status['1']='未中标';
		$status['2']='已经中标';
		$status['3']='已经出库';
		$status['5']='询价中';
		$this->assign('status',$status);
		$this->display();
	}//end more_edit
	
	/**
	*中标操作
	*@date 2013-8-20
	*@time 下午11:33:03
	*/
	function do_more_edit() {
	//中标操作
		if($_POST['status']>1){
			$_POST['is_show']=3;
		}
		$url=U("Depot/more",unserialize($_REQUEST['urlencode']));
		$dao=M("DepotMore");
		parent::update($dao,$url);
	}//end do_more_edit
	
	/**
	 *获取参与进价的供应商数量
	 *@date 2013-8-20
	 *@time 下午4:15:49
	 */
	function supnumber($ids=array()) {
		//获取参与进价的供应商数量
		$where=array();
		if(is_array($ids)){
			$where['depot_id']=array('IN',implode(',', $ids));
		}else{
			$where['depot_id']=$ids;
		}
		$dao=M("DepotMore");
		$data=$dao->field('depot_id,count(`depot_id`) num')->where($where)->group('depot_id')->select();
		return field2key($data,'depot_id');
	}//end supnumber
	


	/**
	 *获取订单内商品的信息
	 *@date 2013-8-16
	 *@time 下午3:23:31
	 */
	function getgoods($oids=array(),$goods=array(),$field='product_id') {
		//获取订单内商品的名称
		if(empty($oids)&&empty($goods)) return false;
		$dao=M('OrderGoods');
		$where=array();
		if(is_array($oids)){
			$oids=array_flip($oids);
			$oids=array_flip($oids);
			$where['order_id']=array('IN',implode(',',$oids));
		}else{
			if($oids){
				$where['order_id']=$oids;
			}
		}
		if(is_array($goods)){
			$where[$field]=array('IN',implode(',',$goods));
		}else{
			$where[$field]=$goods;
		}
		$depot=$dao->field('order_id,product_id,id goods_id,product_name,product_psn,number')->where ( $where )->group($field)->select ();
		return field2key($depot,$field);
	}//end getgoods
	
	/**
	*获取商品的名称
	*@date 2013-8-30
	*@time 上午10:40:20
	*/
	function getproductinfo($pids=array(),$field='*') {
	//获取商品的名称
		if(empty($pids)) return false;
		$where=array();
		$dao=M('ProductDescription');
		if(is_array($pids)){
			$where['product_id']=array('IN',implode(',',$pids));
		}else{
			$where['product_id']=$pids;
		}
		$products=$dao->field($field)->where ( $where )->select ();
		return field2key($products,"product_id");
	}//end getproductinfo
	
	/**
	*获取供应商名单
	*@date 2013-8-20
	*@time 下午10:57:48
	*/
	function getsups($ids=array()) {
	//获取供应商名单
		if(empty($ids)) return false;
		$dao=M("ProductSuppliers");
		$where=array();
		$where['status']=1;
		if(is_array($ids)){
			$where['id']=array('IN',implode(',', $ids));
		}else{
			$where['id']=$ids;
		}
		$list=$dao->where($where)->select();
		return field2key($list,'id');
	}//end getsups
	
	/**
	*设定需要招标的供应商
	*@date 2013-8-19
	*@time 下午3:50:40
	*/
	function set_suppliers() {
	//设定需要招标的供应商
		$dids=$_REQUEST['ids'];
		$dids=is_array($dids)?implode(',', $dids):$dids;
		$this->assign('dids',$dids);
		$dao=M("ProductSuppliers");
		$where=array();
		$where['status']=1;
		$list=$dao->where($where)->select();
		$this->assign('list',$list);
		$dmdao=M("DepotMore");
		$where=array();
		$where['depot_id']=$dids;
		$selsid=$dmdao->field('suppliers,status')->where($where)->group('suppliers')->select();
		$selsid=field2key($selsid, 'suppliers');
		$suppliers=arr_to_explode($selsid,'suppliers','');
		$this->assign('suppliers',$suppliers);
		$this->assign('selsid',$selsid);
		$status=array();
		//-1未出价 0缺货  1未中标  2已经中标  3已经出库
		$status['-1']='未出价';
		$status['0']='缺货';
		$status['1']='未中标';
		$status['2']='已经中标';
		$status['3']='已经出库';
		$status['5']='询价中';
		$this->assign('status',$status);
		$this->display();
	}//end set_suppliers
	
	/**
	*写入竞价表
	*@date 2013-8-19
	*@time 下午5:39:58
	*/
	function do_setsuppliers() {
	//写入
		$sid=$_REQUEST['sid'];
		$dids=$_REQUEST['dids'];
		$deport=self::getdepotinfo($dids);
		$deport=$deport[$dids];
		$dao=M("DepotMore");
		$vo=array();
		$vo['depot_id']=$dids;
		$vo['status']='-1';
		$vo['ctime']=time();
		$vo['version']=0;
		$vo['ok_number']=0;
		$vo['ok_price']=0;
		$vo['depot_code']=$deport['code'];
		if(empty($sid)&&!empty($_REQUEST['sids'])){
			foreach ($_REQUEST['sids'] as $sid){
				$data=array();
				$data=$vo;
				$data['suppliers']=$sid;
				$res=$dao->add($data);
			}
		}else{
			$data=$vo;
			$data['suppliers']=$sid;
			$res=$dao->add($data);
		}
		if($res){
			$result=array('statusCode'=>200,'message'=>'操作成功');
			$result['forwardUrl']=U('Depot/set_suppliers',array('ids'=>$dids));
			$result['dialogId']="Depot_depot_{$dids}";
			exit(json_encode($result));
		}else{
			$this->_ajm('300','操作失败，请重试'.$dao->getLastSql());
		}
	}//end function_name
	
// 	/**
// 	*导出出库单
// 	*@date 2013-8-22
// 	*@time 上午11:58:22
// 	*/
// 	function out() {
// 	//导出出库单
// 		$where=array();
// 		//$where['status']=1;
// 		$dao=M("Depot");
// 		$sql="SELECT COUNT(*) tp_count FROM (SELECT COUNT(id) AS tp_count FROM `depot` GROUP BY ymd) as c";
// 		$count=$dao->query($sql);
// 		$count=$count['0']['tp_count'];
// 		$this->assign("totalCount",$count);
// 		$list=array();
// 		$field="code,ctime,mtime,sum(`number`) number,ymd";
// 		if($count>0){
// 			$p=$_REQUEST[C('VAR_PAGE')]?$_REQUEST[C('VAR_PAGE')]:1;
// 			$first=$p==1?time():mktime(date('H'),date('i'),date('s'),date('n')-$p, date('d'),date('Y'));
// 			$daynum=date("t",$first);
// 			$this->assign('numPerPage',$daynum);
// 			$end=$p==1?mktime(date('H'),date('i'),date('s'),date('n')-1, date('d'),date('Y')):mktime(date('H'),date('i'),date('s'),date('n')-$p-1, date('d'),date('Y'));
// 			$where['ctime']=array(array('lt',$first),array('gt',$end),'AND');
// 			$list=$dao->field($field)->where($where)->group('code')->order($this->order)->select();
// 		}
// 		$list=field2key($list,'ymd',2);
// 		$this->assign('list',$list);
// 		$codes=arr_to_explode($list,'code');
// 		$hitgoods=self::gethitgoods($codes,'count');
// 		$this->assign('hitgoods',$hitgoods);
// 		$suppliers=self::getgysdepot('',$codes,'1');
// 		//dump($suppliers);
// 		$this->assign('suppliers',$suppliers);
// 		self::_suppliers();
// 		$this->display();
// 	}//end out
	
	function out(){
		$model = M('OrderExt');
		$where = array();
		$where['element'] = 'courier_number';
		$_REQUEST['keys'] ? $where['_string'] = "value like '%".$_REQUEST['keys']."%'" : false;
		$count = $model->where($where)->count();
		$page = new Page($count, $this->listrows);
		$limit = "$page->firstRow,$page->listRows";
		$list = $model->where($where)->order('id DESC')->limit($limit)->select();
// 		echo $model->getLastSql();
		$this->assign('list',$list);
		$this->assign ( "totalCount", $count );
		$this->assign('keywords',$_REQUEST['keys']);
		$this->display();
	}
	
	/**
	*获取所有供应商
	*@date 2013-8-29
	*@time 下午2:49:12
	*/
	protected function _suppliers() {
	//获取所有供应商
		$dao=M("productSuppliers");
		$productSuppliers=$dao->select();
		$this->assign('productSuppliers',field2key($productSuppliers,'id'));
	}//end _suppliers
	
	/**
	*获取出库单中标商品数量
	*@param array $arr 出库单号
	*@date 2013-8-24
	*@time 下午4:33:53
	*/
	function gethitgoods($arr=array(),$iscount='') {
	//获取出库单中标商品数量
		$newarr=array();
		$dao=M("DepotMore");
		$where=array();
		if(is_array($arr)){
			$where['depot_code']=array('IN',implode(',',$arr));
		}else{
			$where['depot_code']=$arr;
		}
		$where['status']=array('gt',1);
		if($iscount=='count'){
			$field='depot_code,sum(ok_number) alloknumber';
			$data=$dao->field($field)->where($where)->group('depot_code')->select();
			$newarr=field2key($data,'depot_code');
		}else{
			$field="depot_code,suppliers,depot_id,content,price,status,ctime,version,ok_number,ok_price,is_show";
			$data=$dao->field($field)->where($where)->select();
			$newarr=field2key($data,'depot_code');
		}
		return $newarr;
	}//end gethitgoods
	
	/**
	*获取每一个供应商的出货单
	*@date 2013-8-24
	*@time 下午5:05:02
	*/
	function getgysdepot($suppliers='',$code='',$iscount='') {
	//获取每一个供应商的出货单统计
		$newarr=array();
		$dao=M("DepotMore");
		$where=array();
		if(is_array($suppliers)){
			$where['suppliers']=array('IN',implode(',',$suppliers));
		}else{
			if($suppliers){
				$where['suppliers']=$suppliers;
			}
		}
		if(is_array($code)){
			$where['depot_code']=array('IN',implode(',',$code));
		}else{
			if($code){
				$where['depot_code']=$code;
			}
		}
		$where['status']=array('gt',1);
		$res=array();
		if($iscount!=''){
			$data=$dao->field('suppliers,sum(`ok_number`) ok_number,depot_code')->where($where)->group('suppliers')->select();
			//dump($dao->getLastSql());
			foreach ($data as $v){
				$res[$v['depot_code']][$v['suppliers']]=$v['ok_number'];
			}
		}else{
			$data=$dao->where($where)->select();
			$res=field2key($data,'suppliers',2);
		}
		return $res;
	}//end getgysdepot
	
	/**
	*生成每个供应商的出库单
	*@date 2013-8-24
	*@time 下午5:05:46
	*/
	function gysdepot() {
	//生成每个供应商的出库单
		$code=$_REQUEST['code'];
		$suppliers=intval($_REQUEST['suppliers']);
		$dao=D("DepotV");
		$where=array();
		$where['suppliers']=$suppliers;
		$where['depot_code']=$code;
		$field="more_id,suppliers,depot_code,sum(dm.ok_number) ok_number,ok_price,more_status,product_id";
		$list=$dao->field($field)->where($where)->group('product_id')->select();
		$pids=arr_to_explode($list,'product_id');
		$goodsids=arr_to_explode($list,'product_id');
		$product=self::getproductinfo($goodsids,'name,alias,product_id');
		$this->assign('product',$product);
		$this->assign('list',$list);
		//dump($list);
		$status=array();
		//-1未出价 0缺货  1未中标  2已经中标  3已经出库
		$status['-1']='未出价';
		$status['0']='缺货';
		$status['1']='未中标';
		$status['2']='已经中标';
		$status['3']='已经出库';
		$status['5']='询价中';
		$this->assign('status',$status);
		$this->display();
	}//end gysdepot
	
	/**
	 *获取出库单号
	 *@date 2013-8-16
	 *@time 下午2:30:30
	 *@param array $dids ID 的数组
	 *@param return array
	 */
	function getdepotinfo($dids=array()) {
		//获取订单的出库单号
		if(empty($dids)) return false;
		$where=array();
		if(is_array($dids)){
			$where['id']=array('IN',implode(',',$dids));
		}else{
			$where['id']=$dids;
		}
		//$where['status']=array('GT',0);
		$dao=M('Depot');
		$depot=$dao->field('id,code,order_id,product_id,goods_id,number')->where ( $where )->select ();
		return field2key($depot,'id');
	}//end getdepotinfo
	
	/**
	*获取订单的出库单ID
	*@date 2013-8-20
	*@time 下午10:25:05
	*/
	function getorder_depot($oids=array(),$field=false) {
	//获取订单的出库单ID
		if(empty($oids)) return false;
		$where=array();
		if(is_array($oids)){
			$where['order_id']=array('IN',implode(',',$oids));
		}else{
			$where['order_id']=$oids;
		}
		$where['status']=array('GT',0);
		$dao=M('Depot');
		$depot=$dao->field('id,code,order_id,product_id,goods_id,number')->where ( $where )->select ();
		$depot=$field?field2key($depot,$field):$depot;
		return $depot;
	}//end getorder_depot
	
	/**
	 * 通用编辑
	 */
	function edit(){
		$ids='';
		if(!empty($_REQUEST['ids'])){
			$ids=implode(',', $_REQUEST['ids']);
			$this->assign('ids',$ids);
		}
		$id=intval($_REQUEST['id']);
		$this->assign('oid',$_REQUEST['oid']);
		if(empty($id)&&empty($ids)){
			$this->_ajm(300,'请选择一条需要修改的数据',0);
		}
		if(empty($ids)){
			$dao=M('Depot');
			$info=$dao->where("id=".$id)->find();
			if(empty($info)){
				$this->_ajm(300,'操作错误，请重试',0);
			}
			$this->assign("info",$info);
		}
		$this->display();
	}

	
	/**
	*修改数据
	*@date 2013-8-20
	*@time 下午5:51:02
	*/
	function update() {
	//修改数据
		$_POST['mtime']=time();
		$url='';
		if($_REQUEST['oid']){
			$url=U("Order/depotdetail",array('oid'=>$_REQUEST['oid']));
		}
		if(!empty($_POST['id'])){
			parent::update($this->dao,$url);
		}else{
			$dao   =$this->dao;
			if(empty($_POST['ids'])){
				$this->_ajm(300,'请选择一条需要修改的数据',0);
			}
			$res=$where=array();
			$where['id']=array('IN',$_POST['ids']);
			$data['status']=$_POST['status'];
			$data['mtime']=time();
			$data=$dao->where($where)->save($data);
			if($res===FALSE){
				$this->_ajm(300,'数据写入失败，请重试',0);
			}else{
				$this->_ajm(200,'数据修改成功',0,$url);
			}
		}
	}//end update
	
}//end DepotAction